package xyz.hubery.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xyz.hubery.domain.ExchangeTrade;
import xyz.hubery.dto.CreateKLineDto;
import xyz.hubery.service.TradeKlineService;

import java.util.List;

@Service
@Slf4j
public class StreamMessageListener {


    /**
     * 接收交易记录，用于更新k线
     * @param exchangeTrades
     */
    @StreamListener("exchange-trades-in")
    public void exchangeTradesIn(List<ExchangeTrade> exchangeTrades) {
        log.info("接收到交易记录:{}",exchangeTrades);
        if(!CollectionUtils.isEmpty(exchangeTrades)) {
            for (ExchangeTrade exchangeTrade : exchangeTrades) {
                CreateKLineDto kLineDto = exchangeTrade2KlineData(exchangeTrade);
                TradeKlineService.addData(kLineDto);
            }
        }
    }


    private CreateKLineDto exchangeTrade2KlineData(ExchangeTrade exchangeTrade) {
        CreateKLineDto kLineDto = new CreateKLineDto();
        kLineDto.setPrice(exchangeTrade.getPrice());
        kLineDto.setSymbol(exchangeTrade.getSymbol());
        kLineDto.setVolume(exchangeTrade.getVolume());
        return kLineDto;
    }

}
